package com.cogent.report.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.cogent.business.domain.ReportResultDomain;
import com.cogent.business.domain.ReportStatResultDomain;
import com.cogent.common.date.JodaDateUtil;
import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dao.domain.QueryParamDomain;
import com.cogent.dao.facade.BussinessDaoFacade;
import com.cogent.dao.type.DistrictControl;
import com.cogent.report.display.IReportDisplay;
import com.cogent.report.display.impl.AgeDisplayImpl;
import com.cogent.report.display.impl.DateDisplayImpl;
import com.cogent.report.display.impl.DateTimeDisplayImpl;
import com.cogent.report.display.impl.EnumDisplayImpl;
import com.cogent.report.display.impl.LevelSelectDisplayImpl;
import com.cogent.report.display.impl.MutiEnumDisplayImpl;
import com.cogent.report.display.impl.TextDisplayImpl;
import com.cogent.report.domain.ReportDisplayFieldDomain;
import com.cogent.report.domain.ReportDomain;
import com.cogent.report.domain.ReportSchemaFieldDomain;
import com.cogent.report.facade.IReportFacade;
import com.cogent.report.stat.IListReport;
import com.cogent.report.stat.IStaticReport;

public class ReportFacadeImpl implements IReportFacade {
	private static HashMap _CUR_QUERY_MAP = new HashMap();
	private static HashMap _CUR_DISPLAY_TYPE_MAP = new HashMap();
	private static HashMap _CUR_STATIC_TYPE_MAP = new HashMap();
	private static HashMap _CUR_MUTI_LIST_TYPE_MAP = new HashMap();
	
	static {
		_CUR_QUERY_MAP.put("EQUAL", "1");
		_CUR_QUERY_MAP.put("GREATER", "2");
		_CUR_QUERY_MAP.put("GREATERANDEQUAL", "3");
		_CUR_QUERY_MAP.put("LESS", "4");
		_CUR_QUERY_MAP.put("LESSANDEQUAL", "5");
		_CUR_QUERY_MAP.put("LEFTMATCH", "6");
		_CUR_QUERY_MAP.put("RIGHTMATCH", "7");
		_CUR_QUERY_MAP.put("ALLFUZZYMATCH", "8");
		_CUR_QUERY_MAP.put("INARRAY", "9");
		_CUR_QUERY_MAP.put("LIKEINARRAY", "10");
	}
	
	static {
		_CUR_DISPLAY_TYPE_MAP.put("TEXT", new TextDisplayImpl());
		_CUR_DISPLAY_TYPE_MAP.put("DATE", new DateDisplayImpl());
		_CUR_DISPLAY_TYPE_MAP.put("ENUM", new EnumDisplayImpl());
		_CUR_DISPLAY_TYPE_MAP.put("AGE", new AgeDisplayImpl());
		_CUR_DISPLAY_TYPE_MAP.put("DATETIME", new DateTimeDisplayImpl());
		_CUR_DISPLAY_TYPE_MAP.put("LEVELSELECT", new LevelSelectDisplayImpl());
		_CUR_DISPLAY_TYPE_MAP.put("MUTIENUM", new MutiEnumDisplayImpl());
	}
	
	static {
	}
	
	static {
	}

	public List getQueryList(ReportDomain reportDomain, HashMap formMap) {
		List ret = new ArrayList();
		List schemlist = reportDomain.getSchemaFieldList();
		Iterator it = schemlist.iterator();
		while(it.hasNext()) {
			ReportSchemaFieldDomain schemaFieldDomain = (ReportSchemaFieldDomain)it.next();
			if((MapUtil.isEmpty(formMap, schemaFieldDomain.getName()))
						|| (MapUtil.getStringValue(formMap, schemaFieldDomain.getName()).equals(schemaFieldDomain.getNullValue())))
				continue;
			if(!StringUtil.isEmpty(schemaFieldDomain.getType())) {
				if(schemaFieldDomain.getType().equals("AGE"))
					formMap.put(schemaFieldDomain.getName() + schemaFieldDomain.getType(), JodaDateUtil.getShortDate(JodaDateUtil.addYear(JodaDateUtil.getCurDate(),
								1-MapUtil.getIntValue(formMap, schemaFieldDomain.getName()))));
			} 
			if(schemaFieldDomain.getName().equals("districtcode")) {
				String districtcode = MapUtil.getStringValue(formMap, "districtcode");
				ret.add(new QueryParamDomain(schemaFieldDomain.getTablename(), schemaFieldDomain.getDbfield(), 
						schemaFieldDomain.getMapfield(), DistrictControl.getControlCode(districtcode),
						DistrictControl.getQueryType(districtcode)));	
			}else
				ret.add(new QueryParamDomain(schemaFieldDomain.getTablename(), schemaFieldDomain.getDbfield(), 
							schemaFieldDomain.getMapfield(), MapUtil.getStringValue(formMap, schemaFieldDomain.getName() + schemaFieldDomain.getType())
							, MapUtil.getIntValue(_CUR_QUERY_MAP, schemaFieldDomain.getSearchType(), 1)));			
		}
		
		return ret;
	}
	
	public long getDefalutShowValue(List displayList) {
		long num = 0;
		for(int i=0; i<displayList.size(); i++) {
			 ReportDisplayFieldDomain displayFieldDomain = (ReportDisplayFieldDomain)displayList.get(i);
			 if(displayFieldDomain.isIsdefault())
				 num += (long)Math.pow(new Double(2).doubleValue(), new Double(i).doubleValue());
		}
		
		return num;
	}
	
	public String getDisplayValue(ReportDisplayFieldDomain displayFieldDomain, HashMap dataMap) {
		IReportDisplay reportDisplay = (IReportDisplay)_CUR_DISPLAY_TYPE_MAP.get(displayFieldDomain.getDatatype());
		return reportDisplay.getDisplayValue(displayFieldDomain, dataMap);
	}
	
	public ReportResultDomain getList(BussinessDaoFacade bussinessFacade, ReportDomain reportDomain, HashMap formMap) {
		/*IListReport listReport = new ListReportImpl();
		return listReport.getList(bussinessFacade, reportDomain, formMap);*/
		return null;
	}
	
	public ReportResultDomain getMutilList(BussinessDaoFacade bussinessFacade, ReportDomain reportDomain, HashMap formMap) {
		IListReport listReport = (IListReport)_CUR_MUTI_LIST_TYPE_MAP.get(reportDomain.getType().toUpperCase());
		return listReport.getList(bussinessFacade, reportDomain, formMap);
	}
	
	public ReportStatResultDomain doStatic(BussinessDaoFacade bussinessFacade, ReportDomain reportDomain, HashMap formMap) {
		IStaticReport staticReport = (IStaticReport)_CUR_STATIC_TYPE_MAP.get(reportDomain.getType().toUpperCase());
		return staticReport.doStatic(bussinessFacade, reportDomain, formMap);
	}
}
